/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

/*
    SPDX-FileCopyrightText: 2011 Martin *Gräßlin <mgraesslin@kde.org>
    SPDX-FileCopyrightText: 2012 Gregor Taetzner <gregor@freenet.de>
    SPDX-FileCopyrightText: 2014 Sebastian Kügler <sebas@kde.org>
    SPDX-FileCopyrightText: 2015-2018 Eike Hein <hein@kde.org>
    SPDX-FileCopyrightText: 2021 Mikel Johnson <mikel5764@gmail.com>
    SPDX-FileCopyrightText: 2021 Noah Davis <noahadvs@gmail.com>
    SPDX-FileCopyrightText: 2022 Nate Graham <nate@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
 */
import QtQuick 2.15
import QtQml 2.15
import QtQuick.Layouts 1.15
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 3.0 as PC3
import org.kde.kirigami 2.16 as Kirigami

AbstractKickoffItemDelegate {
    id: root

    required property string appIconName

    property real scaleFactor: PlasmaCore.Units.devicePixelRatio;
    property bool compact: Kirigami.Settings.tabletMode ? false : plasmoid.configuration.compactMode
    property int itemH: 48 * scaleFactor
    property bool isDefaultItemColor: true

    topPadding: compact ? PlasmaCore.Units.mediumSpacing : PlasmaCore.Units.smallSpacing
    bottomPadding: compact ? PlasmaCore.Units.mediumSpacing : PlasmaCore.Units.smallSpacing

    icon.width: 24 * scaleFactor
    icon.height: 24 * scaleFactor

    labelTruncated: label.truncated
    dragIconItem: icon
    isCategoryListItem: true

    function setCategoryListItem() {
        if (isDefaultItemColor) {
            isDefaultItemColor = false
            label.color = "#007aff"
            icon.source = root.appIconName  + "-hover"
        }
    }

    function resetCategoryListItem() {
        if (!isDefaultItemColor) {
            isDefaultItemColor = true
            label.color = PlasmaCore.ColorScope.textColor
            icon.source = root.decoration || root.icon.name || root.icon.source
        }
    }

    contentItem: ColumnLayout {
        anchors.top: parent.top
        RowLayout {
            id: row
            spacing: 18 * scaleFactor
            Layout.minimumHeight: 48 * scaleFactor

            PlasmaCore.IconItem {
                id: icon
                implicitWidth: root.icon.width
                implicitHeight: root.icon.height
                Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter

                animated: false
                usesPlasmaTheme: false
                roundToIconSize: false

                source: root.decoration || root.icon.name || root.icon.source
            }

            PC3.Label {
                id: label

                Layout.leftMargin: 0
                Layout.preferredHeight: implicitHeight
                text: root.text
                textFormat: Text.PlainText
                elide: Text.ElideRight
                maximumLineCount: 1
                color: PlasmaCore.ColorScope.textColor
            }

            Item {
                Layout.fillWidth: true
            }

            PlasmaCore.IconItem {
                id: stateIcon
                Layout.rightMargin: 11 * scaleFactor

                implicitWidth: root.icon.width / 2
                implicitHeight: root.icon.height / 2
                animated: false
                usesPlasmaTheme: true
                source: "go-next-symbolic"
            }
        }

        // application list
        KickoffListView {
            anchors {
                top: row.bottom
                left: parent.left
                right:parent.right
                rightMargin: stateIcon.implicitWidth
            }

            id: applicationsListView
            objectName: "applicationsListView"
            mainContentView: true
            visible: false
            model: plasmoid.rootItem.rootModel.modelForRow(index)

            // needed otherwise app displayed at top-level will show a first character as group.
            section.property: ""
            hasSectionView: false
            isCategoryList: true
        }
    }

    onMouseEntered: setCategoryListItem()
    onMouseExited: resetCategoryListItem()
    onIsExpandedChanged: {
        stateIcon.source = isExpanded ? "expand" : "go-next-symbolic"
        applicationsListView.visible = isExpanded
        applicationsListView.implicitHeight = isExpanded ? (itemH ) * applicationsListView.model.count : 0
        root.height = isExpanded ? applicationsListView.implicitHeight + itemH : itemH
    }

    Loader {
        id: separatorLoader

        anchors.left: root.left
        anchors.right: root.right
        anchors.verticalCenter: root.verticalCenter

        active: root.isSeparator

        asynchronous: false
        sourceComponent: PlasmaCore.SvgItem {
            width: parent.width
            height: KickoffSingleton.lineSvg.horLineHeight

            svg: KickoffSingleton.lineSvg
            elementId: "horizontal-line"
        }
    }
}
